package com.pysoft.safe.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pysoft.common.util.Identities;
import com.pysoft.safe.dao.entity.UserEntity;
import com.pysoft.safe.dao.mapper.UserMapper;
import com.pysoft.safe.dao.mapper.UserRoleMapper;
import com.pysoft.safe.service.UserService;

public class UserServiceImpl implements UserService {

	private UserMapper userDao;
	private UserRoleMapper userRoleDao;
	
	@Override
	public List<UserEntity> query() {
		return userDao.query()	;
	}

	@Override
	public int count() {
		return userDao.count();
	}
	
	
	@Override
	public String addUser(UserEntity user) {
		user.setId(Identities.uuid2());
		userDao.insert(user);
		return user.getId();
	}
	
	@Override
	public void modifyUser(UserEntity user) {
		userDao.update(user);
	}
	
	@Override
	public void deleteUser(List<String> idList) {
		for(String id:idList) {
			userDao.delete(id);
		}
	}
	
	@Override
	public UserEntity getUser(String id) {
		return userDao.get(id);
	}
	
	@Override
	public void relatingRole(List<String> userIdList, List<String> roleIdList) {
		for(String userId:userIdList) {
			userRoleDao.deleteByUser(userId);
			for(String roleId:roleIdList) {
				Map<String, Object> entity = new HashMap<String, Object>();
				entity.put("roleId", roleId);
				entity.put("userId", userId);
				userRoleDao.insert(entity);
			}
		}
	}
	
	@Override
	public void cancelRelatingRole(List<String> userIdList) {
		for(String userId:userIdList) {
			userRoleDao.deleteByUser(userId);
		}
	}
	
	@Override
	public List<String> getRelatingRoles(String userId) {
		return userRoleDao.queryRole(userId);
	}
	
	public UserMapper getUserDao() {
		return userDao;
	}

	public void setUserDao(UserMapper userDao) {
		this.userDao = userDao;
	}

	public UserRoleMapper getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(UserRoleMapper userRoleDao) {
		this.userRoleDao = userRoleDao;
	}
	
	
}
